package com.example.hdwx.myapplication.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.hdwx.myapplication.R;

import com.example.hdwx.myapplication.eneity.goodChoice_eneitys.SmallFragmentsEneity;
import com.example.hdwx.myapplication.untils.UniversalImage;

import java.util.List;

/**
 * 优选碎片中横向滑动的小碎片适配器
 */
public class Goodchoice_Small_Fragment_Adapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    Context context;
    List<SmallFragmentsEneity> list;

    public Goodchoice_Small_Fragment_Adapter(Context context,List<SmallFragmentsEneity> list){
        this.context = context;
        this.list = list;
    }

    @Override
    public int getItemViewType(int position) {
        if (position==list.size()){
            return R.layout.goodchoice_small_fragment_item2;
        }else {
            return R.layout.goodchoice_small_fragment_item1;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //获取item布局
        View view = LayoutInflater.from(context).inflate(viewType, parent, false);
        if (viewType==R.layout.goodchoice_small_fragment_item1){
            return new Goodcholce_small_Item1(view);
        }else {
            return new Goodcholce_small_Item2(view);
        }
    }

    //控件赋值的相关操作
    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {

        if (holder instanceof Goodcholce_small_Item1){
            SmallFragmentsEneity eneity = list.get(position);
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //// TODO: 17-2-14 跳转到商品详情界面
                }
            });
            UniversalImage.image(context,eneity.getCover_image_url(),((Goodcholce_small_Item1) holder).imageView);
            ((Goodcholce_small_Item1) holder).textView.setText(eneity.getDescription());
            ((Goodcholce_small_Item1) holder).textView1.setText("￥"+eneity.getPrice());
        }else if (holder instanceof Goodcholce_small_Item2){
            ((Goodcholce_small_Item2) holder).relativeLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //// TODO: 17-2-13 跳转到商品详情界面 
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return list.size()+1;
    }

    //查找控件
    public class Goodcholce_small_Item1 extends RecyclerView.ViewHolder {

        ImageView imageView;
        TextView textView;
        TextView textView1;
        
        public Goodcholce_small_Item1(View itemView) {
            super(itemView);
            imageView = (ImageView) itemView.findViewById(R.id.goodChoice_small_item1_imageView);
            textView = (TextView) itemView.findViewById(R.id.goodChoice_small_item1_textView);
            textView1 = (TextView) itemView.findViewById(R.id.goodChoice_small_item1_textView1);
        }
    }
    public class Goodcholce_small_Item2 extends RecyclerView.ViewHolder {

        RelativeLayout relativeLayout;
        
        public Goodcholce_small_Item2(View itemView) {
            super(itemView);
            relativeLayout = (RelativeLayout) itemView.findViewById(R.id.goodChoice_small_item2);
        }
    }
}
